<?php
class QuickNote extends ModelBase
{
	private $tableName = 'myadmin_quick_note';
	
	public static function getInstance()
	{
		return parent::instance(__CLASS__);
	}
	
	public function getQuickNoteRandom()
	{
		$note = array();
		$list = Yii::app()->db->createCommand()
		->select('min(note_id) as min, max(note_id) as max')
		->from($this->tableName)
		->queryRow();
		if ($list) {
			$note_id=rand($list["min"],$list["max"]);
			$note = Yii::app()->db->createCommand()
			->select('*')
			->where('note_id >='.$note_id)
			->from($this->tableName)
			->limit(1)
			->queryRow();
		}		
		return $note;
	}
	public function getQuickNote($note_id)
	{
		$note = array();
	
		$note = Yii::app()->db->createCommand()
			->select('*')
			->where('note_id ='.$note_id)
			->from($this->tableName)
			->queryRow();	
		return $note;
	}

	
	
	public function getQuickNotes($offset=0,$pageSize = 10)
	{		
		$user = Yii::app()->db->createCommand()
		->select('a.*,b.user_name as owner_name')
		->from($this->tableName.' a')
		->leftJoin('myadmin_user b', 'b.user_id = a.owner_id')
		->limit($pageSize,$offset)
		->queryAll();
		return $user;
	}
	
	
	public function getQuickNoteCount()
	{
		$user = Yii::app()->db->createCommand()
		->select('count(*) as num')
		->from($this->tableName)
		->queryRow();
		return $user;
	}
	
	public function delQuickNote($nid)
	{
		if(!$nid)
		{
			return false;
		}
		$ret = Yii::app()->db->createCommand()->delete($this->tableName,'note_id = '.$nid);
		return $ret;
	}
	
	public function insertQuickNote($data)
	{
		$ret = Yii::app()->db->createCommand()->insert($this->tableName,$data);
		return $ret; 
	}
	public function updateQuickNote($data,$note_id)
	{
		$ret = Yii::app()->db->createCommand()->update($this->tableName,$data,'note_id='.$note_id);
		return $ret; 
	}
}